package demo1;

import java.util.Arrays;
import java.util.Comparator;

/**
 * Created with IntelliJ IDEA
 * Description：Comparable接口的使用
 * User：chenshu
 * Date：2023-10-21
 * Time：19:59
 */
class Student implements Comparable<Student> {
    public String name;
    public int age;
    public double score;

    public Student(String name, int age, double score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        //return this.age - o.age;
        //return this.name.compareTo(o.name);
        return (int)(this.score - o.score);
    }
}
public class TestDemo {
    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("zhangsan", 18, 78.9);
        students[1] = new Student("lisi", 38, 18.9);
        students[2] = new Student("wangwu", 78, 88.9);

        Arrays.sort(students);

        System.out.println(Arrays.toString(students));
    }
}
